<?php
App::uses('AppController', 'Controller');
/**
 * Articles Controller
 *
 * @property Article $Article
 * @property PaginatorComponent $Paginator
 */
class ProductsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	var $uses = array('Product', 'ProductCategory', 'ProductBrand');	
	public $components = array('Paginator');

	/**
	 * index method
	 *
	 * @return void
	 */
	public function index() {
        $this->ProductBrand->recursive = 0;
		$this->set('brands', $this->ProductBrand->find('all'));
	}
    
    public function search() {
        $name = $this->request->query('name');
        $this->paginate = array(
			'conditions' => array('Product.name like' => '%' . $name . '%'),
			'limit' => 15,
			'order' => array(
				'Product.created_date' => 'DESC'			
			),
			'recursive' => -1
		);
        
		$this->set('products', $this->Paginator->paginate());
	}

	/**
	 * view method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function view($id = null) {
		if (!$this->Product->exists($id)) {
			throw new NotFoundException(__('Invalid product'));
		}
	
		$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
		$this->Product->recursive = 1;
		$product = $this->Product->find('first', $options);
		$this->set('product', $product);	

		$options = array(
            'conditions' => array('ProductCategory.product_brand_id' => $product['ProductCategory']['product_brand_id']),
            'order' => 'category_name_en ASC'
        );
		$this->set('categoryList', $this->ProductCategory->find('all', $options));		
	}

	/**
	 * view method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function category($id = null) 
	{
		if (!$this->ProductCategory->exists($id)) {
			throw new NotFoundException(__('Invalid article'));
		}
		
		$this->ProductCategory->recursive = 0;
		$categoryDetail = $this->ProductCategory->findById($id);
		$this->set('categoryDetail', $categoryDetail);

		$options = array(
            'conditions' => array('ProductCategory.product_brand_id' => $categoryDetail['ProductCategory']['product_brand_id']),
            'order' => 'category_name_en ASC'
        );
        $this->set('categoryList', $this->ProductCategory->find('all', $options));
		
		$options = array('conditions' => array('Product.product_category_id' => $id));
		$this->paginate = array(
			'conditions' => array('product_category_id' => $id),
			'limit' => 15,
			'order' => array(
				'Product.name_en' => 'ASC'			
			),
			'recursive' => -1
		);
		
        $this->Paginator->settings = $this->paginate;
		$this->set('products', $this->Paginator->paginate('Product'));
		
	}
}
